﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using YJYpublicClass;
using System.Windows;

namespace WindowsFormsApp2.JiayuModelset
{
    public partial class Frm_模型操作 : Form
    {
        ModelCaozuo tempdev = null;
        public Frm_模型操作(ModelCaozuo obj)
        {
            InitializeComponent();
            tempdev = obj;
        }

        private void Frm_模型操作_Load(object sender, EventArgs e)
        {
            this.propertyGrid1.SelectedObject = tempdev.modelone;
            //
            //block窗口加载流程
            this.cogToolBlockEditV21.Subject = tempdev.modelone.mainvppblock;
            this.cogDisplayToolbarV21.Display = cogRecordDisplay1;
            this.cogDisplayStatusBarV21.Display = cogRecordDisplay1;

            timer1.Enabled = true;
 
        }

        private void btn_加载模型_Click(object sender, EventArgs e)
        {
            //打开路径，弹窗提示，加载模型会覆盖当前模型
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.InitialDirectory = 外设.SysPras.Vpppath;
            openFileDialog1.Filter = "ALL Image Files|*.yjye";
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string path = openFileDialog1.FileName;
                tempdev.Loadmode(path, out tempdev.modelone);
                //更新显示
                this.propertyGrid1.SelectedObject = tempdev.modelone;
                this.cogToolBlockEditV21.Subject = tempdev.modelone.mainvppblock;
            
            }

        }

        private void btn_保存模型_Click(object sender, EventArgs e)
        {
            try
            {
             
                string path = 外设.SysPras.Vpppath + "\\" + tempdev.modelone._款式名称 + ".yjye";
                if (File.Exists(path))
                {
                    // DialogResult result = System.Windows.Forms.MessageBox.Show("是否覆盖现有模型?", "是", MessageBoxButtons.YesNo);
                    if (DialogResult.Yes == System.Windows.Forms.MessageBox.Show("是否覆盖现有模型?", "是", MessageBoxButtons.YesNo))
                    {
                        tempdev.modelone.mainvppblock = cogToolBlockEditV21.Subject;
          
                        tempdev.Savemode(path, tempdev.modelone);
                    }

                }
                else
                {
                    tempdev.modelone.mainvppblock = cogToolBlockEditV21.Subject;
      
                    tempdev.Savemode(path, tempdev.modelone);
                }
                //写数据           
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message);
            }
        }

        private void btn_模型另存_Click(object sender, EventArgs e)
        {
            //修改名字就可以保存
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            try
            {
                tempdev.modelone.mainvppblock = cogToolBlockEditV21.Subject;
                saveFileDialog1.Filter = "ALL Image Files|*.yjye";
                saveFileDialog1.InitialDirectory = 外设.SysPras.Vpppath;
                if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    string path = saveFileDialog1.FileName;
                    //写数据
                    tempdev.Savemode(path, tempdev.modelone);
                }
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message);
            }
        }

        private void btn_新建模型_Click(object sender, EventArgs e)
        {
            //加载一个模型样板，打开到固定的路径下
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.InitialDirectory = 外设.SysPras.Vpppath;
            openFileDialog1.Filter = "ALL Image Files|*.yjye";
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string path = openFileDialog1.FileName;
                tempdev.Loadmode(path, out tempdev.modelone);
                //更新显示
                this.propertyGrid1.SelectedObject = tempdev.modelone;
                this.cogToolBlockEditV21.Subject = tempdev.modelone.mainvppblock;
            }

        }

        private void btn_实时设置_Click(object sender, EventArgs e)
        {
            try
            {
                tempdev.modelone._BFifoAcq_tool.Run();
                cogRecordDisplay1.Record = tempdev.modelone._BFifoAcq_tool.CreateLastRunRecord().SubRecords[0];
                cogRecordDisplay1.Fit();
            }
            catch (Exception EX)
            {

                System.Windows.Forms.MessageBox.Show("打开实时失败" + EX.Message);
            }
        }


   
        private void timer1_Tick(object sender, EventArgs e)
        {
            //更新模型    

        }

        private void btn_更新当前模型_Click(object sender, EventArgs e)
        {
            try
            {
                tempdev.modelone.mainvppblock = cogToolBlockEditV21.Subject;
                tempdev.modelone.init();
                // tempdev.FifoAcq_tool = tempdev.modelone.FifoAcq_tool;
            }
            catch (Exception ex)
            {

                throw;
            }

        }












        private void btn_清除显示_Click(object sender, EventArgs e)
        {
            richTextBox1.Clear();
        }











        private void btn_左侧测试_Click(object sender, EventArgs e)
        {
            int OKNG = 0;
            ResultFunc[] resultFunc = tempdev._上工位检测();
            richTextBox1.AppendText(resultFunc[0].State.ToString());
            richTextBox1.AppendText(resultFunc[0].mes);
            cogRecordDisplay1.Record = tempdev.disprecord1;
            cogRecordDisplay1.Fit();
        }
        private void btn_右侧测试_Click(object sender, EventArgs e)
        {
            int OKNG = 0;
            ResultFunc[] resultFunc = tempdev._下工位检测();
            richTextBox1.AppendText(resultFunc[0].State.ToString());
            richTextBox1.AppendText(resultFunc[0].mes);
            cogRecordDisplay1.Record = tempdev.disprecord1;
            cogRecordDisplay1.Fit();
        }

        private void btn_上2工位测试_Click(object sender, EventArgs e)
        {
            int OKNG = 0;
            ResultFunc[] resultFunc = tempdev._上工位检测();
            richTextBox1.AppendText(resultFunc[1].State.ToString());
            richTextBox1.AppendText(resultFunc[1].mes);
            cogRecordDisplay1.Record = tempdev.disprecord2;
            cogRecordDisplay1.Fit();
        }

        private void btn_下2工位测试_Click(object sender, EventArgs e)
        {
            int OKNG = 0;
            ResultFunc[] resultFunc = tempdev._下工位检测();
            richTextBox1.AppendText(resultFunc[1].State.ToString());
            richTextBox1.AppendText(resultFunc[1].mes);
            cogRecordDisplay1.Record = tempdev.disprecord2;
            cogRecordDisplay1.Fit();
        }
    }

}
